package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.util.DBUtil;



public class UserServiceImpl implements UserService {
    @Override
    public User getUserByIdByPass(String userId, String password) {
        User user=null;
        UserDao userDao=new UserDaoImpl();
        try {

           user= userDao.getUserByIdByPass(userId,password);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(); // 关闭Connection
        }
        return user;
    }

    @Override
    public Integer getUserById(String userId) {
        int result=0;
        UserDao userDao=new UserDaoImpl();
        try {
            result=userDao.getUserById(userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return result;
    }

    @Override
    public Integer saveUser(String userId, String password, String userName, Integer userSex) {
        int result=0;
        UserDao userDao=new UserDaoImpl();
        try {
            DBUtil.beginTransaction();
            result = userDao.saveUser(userId,password,userName,userSex);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return result;
    }
}
